<template>
	<view>
		<navbar title="编辑资料"></navbar>
		<view class="user-wrapper">
			<view class="user-box fa fdc">
				<button class="avatar-box" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
					<image class="avatar" :src="avatar" mode="" v-if="avatar"></image>
					<image class="avatar" src="../../static/avatar.png" mode="" v-else></image>
				</button>
				<view class="text">点击修改头像</view>
			</view>
			<view class="list fa">
				<view class="text">昵称</view>
				<view class="fa">
					<input class="nickname" type="nickname" v-model="nickname" @focus="nicknameFocus" @blur="nicknameBlur" />
				</view>
			</view>
			<view class="list fa">
				<view class="text">手机号码</view>
				<view class="fa">
					<input class="flex1" type="text" v-model="mobile" maxlength="11" />
				</view>
			</view>
			<!-- <view class="list fa jcsb">
				<view class="text">性别</view>
				<picker class="flex1" mode="selector" :range="sexList" :value="sexIndex" @change="sexChange">
					<view class="fa jcsb">
						<view>{{sexList[sexIndex]}}</view>
						<image class="icon" src="/static/right.png" mode=""></image>
					</view>
				</picker>
			</view> -->
		</view>
		<view class="user-wrapper">
			<view class="list fa">
				<view class="text">所属社区</view>
				<view class="fa">{{community}}</view>
			</view>
			<view class="list fa">
				<view class="text">负责楼栋</view>
				<view class="fa">{{building}}</view>
			</view>
		</view>
		<view class="fixed-bottombar">
			<view class="bottombar-box fa jcc">
				<view class="confirm-button fa jcc" @click="saveUserinfo">保存</view>
			</view>
			<view class="fixed-place"></view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				avatar: '',
				nickname: '',
				mobile: '',
				avatar_upload: '',
				nicknameBefore: '',
				sexList: ['男', '女'],
				sexIndex: 0,
				isSex: 1, //1是未设置
				code: '',
				community: '',
				building: ''
			};
		},
		onLoad() {
			this.getUserinfo()
		},
		methods: {
			getUserinfo() {
				this.api.userinfoFunc().then(res => {
					if(res.code == 200) {
						this.avatar = res.data.avatar
						this.nickname = res.data.name
						this.mobile = res.data.phone
						this.community = res.data.takeEstate
						this.building = res.data.takeBuilding
					}
				})
			},
			// 修改头像
			onChooseAvatar(e) {
				this.api.uploadFileFunc(e.detail.avatarUrl).then(res => {
					console.log(res)
					this.avatar = res.url
				})
			},
			nicknameFocus() {
				this.nicknameBefore = this.nickname
			},
			// 修改昵称
			nicknameBlur(e) {
				this.nickname = e.detail.value
				if(this.nickname == this.nicknameBefore) return;
			},
			sexChange(e) {
				this.sexIndex = e.detail.value
			},
			saveUserinfo() {
				if(!this.nickname) return uni.showToast({
					title: '请输入昵称',
					icon: 'none'
				})
				if(this.mobile && !/^[1][3456789][0-9]{9}$/.test(this.mobile)) return uni.showToast({
					title: '请输入正确的手机号',
					icon: 'none'
				})
				this.api.userUpdateFunc({
					avatar: this.avatar,
					name: this.nickname,
					phone: this.mobile
				}).then(res => {
					if(res.code == 200) {
						uni.showToast({
							title: res.msg
						})
						setTimeout(() => {
							uni.navigateBack()
						}, 1500)
					}else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(err => {
					uni.showToast({
						title: '操作失败',
						icon: 'none'
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.user-wrapper{
		margin-top: 20rpx;
		padding: 1rpx 28rpx;
		color: #222;
		background: #fff;
		.user-box{
			margin: 50rpx 0 24rpx;
			.avatar-box{
				padding: 0;
				width: 116rpx;
				height: 116rpx;
				border-radius: 50%;
			}
			.avatar{
				display: block;
				width: 116rpx;
				height: 116rpx;
				border-radius: 50%;
			}
			.text{
				margin-top: 10rpx;
				font-size: 26rpx;
				color: #222;
			}
		}
		.list{
			padding: 32rpx 0;
			border-bottom: 1px solid #eee;
			&:last-child{
				border-bottom: none;
			}
			.text{
				width: 180rpx;
			}
			.icon{
				margin: 0 16rpx 0 12rpx;
				width: 30rpx;
				height: 30rpx;
			}
			.bindmobile{
				padding: 0 16rpx;
				height: 52rpx;
				line-height: 52rpx;
				border: 2rpx solid #999;
				border-radius: 31rpx;
				font-size: 24rpx;
				color: #999;
			}
		}
	}
	.fixed-bottombar{
		.bottombar-box{
			z-index: 98;
			position: fixed;
			bottom: 0;
			bottom: env(safe-area-inset-bottom);
			left: 0;
			right: 0;
			padding: 0 28rpx;
			height: 120rpx;
			z-index: 99;
			background-color: #fff;
			.confirm-button{
				width: 100%;
				height: 88rpx;
				border-radius: 16rpx;
				font-size: 30rpx;
				font-weight: bold;
				color: #fff;
				background: var(--theme);
			}
		}
		.fixed-place{
			height: 120rpx;
		}
	}
</style>
